package com.kotlin.lib.coroutines

import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.system.measureTimeMillis

suspend fun doSomething1(): Int {
    delay(1000)
    return 11
}

suspend fun doSomethingElse(): Int {
    delay(1500)
    return 22
}

fun main() = runBlocking<Unit> {
    val costs = measureTimeMillis {
        val async = async(start = CoroutineStart.LAZY) { doSomething1() }
        val async1 = async(start = CoroutineStart.LAZY) { doSomethingElse() }
        async.start()
        async1.start()//使用惰性的async，手动的启动
        println("result is ${async.await() + async1.await()}")
    }
    println("cost $costs")
}